package com.shiwaixiangcun.customer.module.intelligent

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.ConfigSps
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.DeviceBaseEntity
import com.shiwaixiangcun.customer.network.ResponseConfig
import com.shiwaixiangcun.customer.network.StringDialogCallBack
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.shiwaixiangcun.customer.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_smart_camera_name.*
import kotlinx.android.synthetic.main.top_bar_white.*


/**
 * 编辑智能摄像头名称
 */
class SmartCameraNameActivity : BaseActivity() {

    var cameraId: Long = 0
    var strToken: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_smart_camera_name)
        getBundleData()
        initView()
    }

    private fun getBundleData() {
        cameraId = intent.extras.getLong("camera_id")


    }

    private fun initView() {
        tv_page_name.text = "摄像机名称"
        tv_top_right.text = "确定"
        tv_top_right.visibility = View.VISIBLE
        back_left.setOnClickListener { finish() }

        tv_top_right.setOnClickListener {
            if (edt_camera_name.text.toString().isEmpty()) {
                showToastShort("请输入摄像头的名称")
            } else {
                editCameraName(edt_camera_name.text.toString())
            }
        }


    }

    /**
     * 提交摄像头名称
     */
    private fun editCameraName(name: String) {
        strToken = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_TOKEN, "") as String
        OkGo.put<String>(GlobalApi.editCameraName)

                .params("access_token", strToken)
                .params("cameraId", cameraId)
                .params("name", name)
                .execute(object : StringDialogCallBack(this) {
                    override fun onSuccess(response: Response<String>?) {

                        val type = object : TypeToken<DeviceBaseEntity<Any>>() {}.type
                        val backData = JsonUtil.fromJson<DeviceBaseEntity<Any>>(response?.body(), type)
                        when (backData.responseCode) {
                            ResponseConfig.SUCCESS -> {
                                showToastShort("修改成功")
                                val intent = Intent()
                                // 获取用户计算后的结果
                                intent.putExtra("camera_name", name) //将计算的值回传回去
                                setResult(2, intent)
                                finish()
                            }
                            else -> showToastShort("修改失败")
                        }
                    }

                })
    }
}
